<?php
/**
 * Author: wl
 * Email: 446399160@qq.com
 * DateTime: 2017/04/22 11:33
 */

namespace app\modules\v1\models;
use yii\db\ActiveRecord;
use Yii;

class PaymentCode extends ActiveRecord{

    public static function tableName()
    {
        return '{{payment_code}}';
    }

    public function init()
    {
        parent::init();
    }
    
    public static function getMerchantNo($len,$chars=null){
    	if (is_null($chars)){
    		$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    	}
    	for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
    		$str .= $chars[mt_rand(0, $lc)];
    	}
    	$str = '02'.$str;
    	if(self::checkNo($str)){
    		return $str;
    	}else{
    		$id = static::find()->max('id')+1;
    		return $str.$id;
    	}
    }

    
    public static function checkNo($str){
    	$res = static::findOne(['merchant_no' => $str]);
    	if(empty($res)){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    /**
     * curl请求数据
     * @param 地址     $url
     * @return string|mixed
     */
    public static function httpGet($url,$data = ''){
    	$curl = curl_init();
    	curl_setopt($curl, CURLOPT_URL, $url);
    	if(!empty($data)){
    		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    		curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    		curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    	}
    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    	$data = curl_exec($curl);
    	if (curl_errno($curl)) {
    		return 'ERROR '.curl_error($curl);}
    		curl_close($curl);
    		return $data;
    }
    
}